//
//  COperation.m
//  Delicious
//
//  Created by yilong zhang on 11-11-21.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "COperation.h"

//=========================================================================================================================

@implementation COperation

@synthesize m_TaskDelegate;
@synthesize m_UrlConnection;
@synthesize m_Sponsor;
@synthesize m_Task;
//new
@synthesize m_bIsCancel;

-(id)initWithID:(int)ID Delegate:(id)delegate
{
    self = [super init];
    if (self)
    {
        // Initialization code here.
        m_Task          = nil;
        m_UrlConnection = nil;
        m_bIsCancel     = NO;
        m_Sponsor       = nil;
        
        self.m_TaskDelegate = delegate;
    }
    
    return self;
}

-(void)start
{
    if (![self isCancelled])
    {
        [self StartOperation];
        
        while (self.m_UrlConnection.m_bIsLoaded)
        {
            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; 
        }
    }
}

-(BOOL)isCancelled
{
    ////NSLog(@"isCancelled:%@", (m_bIsCancel) ? (@"YES") : (@"NO"));
    return m_bIsCancel;
}

-(BOOL)isFinished
{
    ////NSLog(@"isFinished:%@", (m_bIsCancel) ? (@"YES") : (@"NO"));
    return m_bIsCancel;
}

-(void)SetOperation:(CTask*)task Request:(CNetWorkRequest*)request
{
    self.m_Task = task;
    self.m_UrlConnection = request;
    self.m_Sponsor = self.m_Task.m_Sponsor;
    self.m_UrlConnection.m_Delegate = self;
}

-(void)StartOperation
{
    [self.m_UrlConnection SetRequestWithUrl:m_Task.m_Url Method:m_Task.m_MType Value:m_Task.m_PostData IsSynchronous:NO OutTime:m_Task.m_nTimeOut];
}

-(void)cancelOperation
{
    [self CancelDelegate];
    [m_UrlConnection Stop];
}

-(void)CancelDelegate
{
    //self.m_bIsCancel = YES;
    self.m_Task.m_Status = Over;
    self.m_Task.m_Operation = nil;
    self.m_Task = nil;
    self.m_Sponsor = nil;
    self.m_UrlConnection = nil;
}

-(void)DidReceiveData:(CNetWorkRequest *)request Data:(NSMutableData *)data
{
    [self.m_Sponsor DidOperationFinished:self Data:data Other:m_Task.m_Data UserInfo:m_Task.m_UserInfo];
    
    [m_TaskDelegate DidTaskFinished:self];
    
    [self CancelDelegate];
}

-(void)DidReceiveError:(CNetWorkRequest *)request Error:(NSError *)error
{
    [self.m_Sponsor DidOperationError:self Error:error Other:m_Task.m_Data UserInfo:m_Task.m_UserInfo];
    
    [m_TaskDelegate DidTaskFinished:self];
    
    [self CancelDelegate];
}

-(void)DidRectiveTimeout:(CNetWorkRequest *)request
{
    [self.m_Sponsor DidOperationTimeOut:self Other:m_Task.m_Data UserInfo:m_Task.m_UserInfo];
    
    [m_TaskDelegate DidTaskFinished:self];
    
    [self CancelDelegate];
}

-(void)DidStopRequestNoNet:(CNetWorkRequest *)request
{
    NSError *error = [NSError errorWithDomain:@"没有网络" code:100 userInfo:nil];
    [self.m_Sponsor DidOperationError:self Error:error Other:m_Task.m_Data UserInfo:m_Task.m_UserInfo];
    
    [m_TaskDelegate DidTaskFinished:self];
    
    [self CancelDelegate];
}

-(void)dealloc
{    
    ////NSLog(@"Operation_dealloc");
    [self CancelDelegate];
    [self.m_Task release];
    
    [super dealloc];
}


@end
